package com.leenleda.ips.util;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.leenleda.ips.BaseActivity;
import com.leenleda.ips.entity.DataEntity;
import com.leenleda.ips.receiver.Constant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileData {


    private static String tag = "FileUtils";

    public static DataEntity getDeviceInfo(int position){
        String deviceInfo = "";
        DataEntity info = null;
        deviceInfo = UShared.getString(Constant.PUBLISH_DATA);//从shredPreference 中获取历史记录
//        ILog.e(tag, "-----getDeviceInfo--" + position + deviceInfo);
        if (!deviceInfo.isEmpty()){//减少文件操作，当数据里能获取时。不打开文件
            info = GsonUtils.getInstance().fromJson(deviceInfo, DataEntity.class);
        } else {
            info = getFileInfo();
        }
        if (info != null)
            return info;
        return new DataEntity();
    }

    /**
     * 获取deviceInfo
     */
    private static DataEntity getFileInfo(){
        String deviceInfo = "";
        LogUtils.e(tag, "====FileInfo=1===" + deviceInfo );
        String path = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_DATA;
        File file = new File(path);
        DataEntity info = null;
        if (!file.exists())
            return new DataEntity();
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            String mimeTypeLine = null ;
            while ((mimeTypeLine = br.readLine()) != null) {
                deviceInfo = deviceInfo+mimeTypeLine;
            }
            isr.close();
            br.close();
            UShared.putString(Constant.PUBLISH_DATA, deviceInfo);
            info = GsonUtils.getInstance().fromJson(deviceInfo, DataEntity.class);
            if (info == null)
                return new DataEntity();
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        }
        file.delete();
        return new DataEntity();
    }

    /**
     * 删除/保存info
     */
    public static boolean setDeviceInfo(DataEntity info){
        String deviceInfo = GsonUtils.getInstance().toJson(info);
        if (deviceInfo.isEmpty())
            return false;
        UShared.putString(Constant.PUBLISH_DATA, deviceInfo);
        LogUtils.e(tag, "===setDeviceInfo===DataEntity=" + deviceInfo );
        String path = getCachePath(BaseActivity.getForegroundActivity()) + Constant.PUBLISH_FILE_DATA;
//        File file = new File(path);
//        if (file.exists())//减少文件操作，当文件存在时不做处理
//            return true;
        LogUtils.e(tag, "====FileInfo=2===" + deviceInfo );
        try {
            FileWriter writer = new FileWriter(path);
            writer.write(deviceInfo);
            writer.flush();
            writer.close();
            LogUtils.i(tag, "====setDeviceInfo===2=" );
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.i(tag, "====setDeviceInfo===23=" + e);
        }
        return false;
    }

    private static String getCachePath(Context context) {
        String path = null;
        try {
            path = Environment.getExternalStorageDirectory().getCanonicalPath() + "/LELDIPS/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("--getCachePath--", "getCrashFilePath: " + path);
        return path;
    }
}
